package jjn.round1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jjn
 * @since 2022/8/18 23:49
 */
public class LeetCode118_PascalsTriangle {

    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<>(numRows * 2);
        for (int i = 0; i < numRows; i++) {
            List<Integer> numbersOnRow = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    numbersOnRow.add(1);
                } else {
                    List<Integer> lastRow = result.get(i - 1);
                    Integer left = lastRow.get(j - 1);
                    Integer right = lastRow.get(j);
                    numbersOnRow.add(left + right);
                }
            }
            result.add(numbersOnRow);
        }
        return result;
    }
}
